package com.weisw.polaris.admin.service.impl;

import com.weisw.polaris.admin.dao.ArticleMapper;
import com.weisw.polaris.admin.dao.MenuMapper;
import com.weisw.polaris.admin.model.Article;
import com.weisw.polaris.admin.model.Menu;
import com.weisw.polaris.admin.service.ArticleService;
import com.weisw.polaris.core.exception.PolarisException;
import com.weisw.polaris.core.page.MybatisPageHelper;
import com.weisw.polaris.core.page.PageRequest;
import com.weisw.polaris.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

//    @Override
//    public Article findByName(String s){
//        return articleMapper.findByName(s);
//    }

    @Override
    public int save(Article record) {
        Integer id = null;
        if(record.getArticleId() == null || record.getArticleId() == 0) {
            // 新增用户
            articleMapper.insert(record);
            id = record.getArticleId();
        } else {
            // 更新用户信息
            articleMapper.updateByPrimaryKey(record);
        }
        // 更新用户角色
        if(id != null && id == 0) {
            return 1;
        }
        return id;
    }

    @Override
    public int delete(Article record) {
        return articleMapper.deleteByPrimaryKey(record.getArticleId());
    }

    @Override
    public int delete(List<Article> records) {
        for(Article record:records) {
            delete(record);
        }
        return 1;
    }

    @Override
    public Article findById(Integer id) {
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) throws PolarisException{
        return MybatisPageHelper.findPage(pageRequest,articleMapper);
    }
}
